package com.zzyl.serve.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDTO;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVO;
import com.zzyl.serve.vo.NursingProjectPlanVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 * 
 * @author zzyl
 * @date 2025-10-13
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVO selectNursingPlanById(Integer id)
    {
        // 获取护理计划信息
        NursingPlan nursingPlan = this.getById(id);

        // 查找中间表
        List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));

        // 数据封装
        NursingPlanVO nursingPlanVo = BeanUtil.toBean(nursingPlan, NursingPlanVO.class);
        List<NursingProjectPlanVO> nursingProjectPlanVoList = nursingProjectPlans.stream().map(nursingProjectPlan -> {
            NursingProjectPlanVO nursingProjectPlanVo = BeanUtil.toBean(nursingProjectPlan, NursingProjectPlanVO.class);
            nursingProjectPlanVo.setProjectId(nursingProjectPlanVo.getProjectId().toString());
            nursingProjectPlanVo.setExecuteCycle(nursingProjectPlanVo.getExecuteCycle().toString());
            return nursingProjectPlanVo;
        }).toList();
        nursingPlanVo.setProjectPlans(nursingProjectPlanVoList);

        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNursingPlan(NursingPlanDTO nursingPlanDto)
    {
        // 数据拷贝
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        // 添加护理计划信息
        nursingPlanMapper.insert(nursingPlan);

        // 批量添加计划中项目信息
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();

        // 如果为 null，赋值为空列表
        if (projectPlans == null) {
            projectPlans = new ArrayList<>();
        }

        projectPlans.forEach(projectPlan -> {
            projectPlan.setPlanId(Long.valueOf(nursingPlan.getId()));
        });
        return nursingProjectPlanService.saveBatch(projectPlans) == true ? 1 : 0;
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int updateNursingPlan(NursingPlanDTO nursingPlanDto)
    {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);

        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (!CollectionUtils.isEmpty(projectPlans)) {
            projectPlans.forEach(projectPlan -> {
                projectPlan.setPlanId(Long.valueOf(nursingPlan.getId()));
            });

            // 删除旧的计划中项目信息
            nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlan.getId()));

            // 保存新的计划中项目信息
            nursingProjectPlanService.saveBatch(projectPlans);
        }
        return this.updateById(nursingPlan) == true ? 1 : 0;
    }

//    /**
//     * 批量删除护理计划
//     * 
//     * @param ids 需要删除的护理计划主键
//     * @return 结果
//     */
//    @Override
//    public int deleteNursingPlanByIds(Integer[] ids)
//    {
//        return this.removeBatchByIds(Arrays.asList(ids)) == true ? 1 : 0;
//    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Integer id)
    {
        List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        if (!CollectionUtils.isEmpty(nursingProjectPlans)) {
            nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        }
        return this.removeById(id) == true ? 1 : 0;
    }

    /**
     * 查询所有护理计划
     *
     * @return
     */
    @Override
    public List<NursingPlan> getPlanAll() {
        return nursingPlanMapper.getPlanAll();
    }
}
